package com.retailshop.shopuser.service.impl;

import com.alibaba.fastjson.JSON;
import com.retailshop.shopuser.entity.RedisUserNo;
import com.retailshop.shopuser.mapper.RedisUserNoMapper;
import com.retailshop.shopuser.mapper.UserNoMapper;
import com.retailshop.shopuser.service.UserRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("UserRedisService")
public class UserRedisServiceImpl implements UserRedisService {

    @Autowired
    UserNoMapper userNoMapper;

    @Autowired
    RedisUserNoMapper redisUserNoMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public Boolean setRedisUserAll() {

        SetMsg("redisMsg","开始建立用户缓存");
        SetState("redisState","1");

        if (!GetState("redisControl")) {

            //读取上一次到到什么地方了
            int maxid = 0;
            try {
                maxid = Integer.parseInt(redisTemplate.opsForValue().get("userjsmaxid").toString());
            } catch (Exception ex) {
            }

            //更新RE
            int reres = userNoMapper.updataRE();
            System.out.println("reres:" + reres);

            System.out.println("maxid:" + maxid);
            if (maxid > -1) {
                List<RedisUserNo> redisUserNoList = redisUserNoMapper.getRedisUserAll(maxid);
                for (int i = 0; i < redisUserNoList.size(); i++) {
                    RedisUserNo redisUserNo = redisUserNoList.get(i);

                    System.out.println("getId:" + redisUserNo.getId());
                    redisTemplate.opsForValue().set("ID" + redisUserNo.getId(), JSON.toJSONString(redisUserNo));
                    redisTemplate.opsForValue().set("US" + redisUserNo.getNo(), JSON.toJSONString(redisUserNo));
                    redisTemplate.opsForValue().set("P" + redisUserNo.getPhone(), JSON.toJSONString(redisUserNo));
                    System.out.println("getId:" + redisUserNo.getId());
                    if (redisUserNo.getId() > maxid) {
                        maxid = redisUserNo.getId();
                    }
                    System.out.println("maxid:" + maxid);
                }
                redisTemplate.opsForValue().set("userjsmaxid", maxid);
            }
        }
        SetMsg("redisMsg","完成建立用户缓存");
        SetState("redisState","0");
        return true;
    }

    @Override
    public Boolean setRedisUser(int id) {
        RedisUserNo redisUserNo = redisUserNoMapper.getRedisUser(id);
        if(redisUserNo!=null)
        {
            System.out.println("getId:" + redisUserNo.getId());
            redisTemplate.opsForValue().set("ID" + redisUserNo.getId(), JSON.toJSONString(redisUserNo));
            redisTemplate.opsForValue().set("US" + redisUserNo.getNo(), JSON.toJSONString(redisUserNo));
            redisTemplate.opsForValue().set("P" + redisUserNo.getPhone(), JSON.toJSONString(redisUserNo));
        }
        return true;
    }

    @Override
    public Boolean delRedisUser(int id) {
        RedisUserNo redisUserNo = redisUserNoMapper.getRedisUser(id);
        if(redisUserNo!=null)
        {
            System.out.println("getId:" + redisUserNo.getId());
            redisTemplate.delete("ID" + redisUserNo.getId());
            redisTemplate.delete("US" + redisUserNo.getNo());
            redisTemplate.delete("P" + redisUserNo.getPhone());
        }
        return true;
    }

    @Override
    public Boolean keepRedisUser() {
        //读取上一次到到什么地方了
        int maxid = 0;
        try {
            maxid = Integer.parseInt(redisTemplate.opsForValue().get("userjsmaxid").toString());
        } catch (Exception ex) {
        }

        System.out.println("maxid:" + maxid);
        return true;
    }

    @Override
    public void SetMsg(String key , String value)
    {
        System.out.println(key+":"+value);
        redisTemplate.opsForValue().set(key,value);
    }

    @Override
    public void SetState(String key , String value)
    {
        System.out.println(key+":"+value);
        redisTemplate.opsForValue().set(key,value);
    }

    @Override
    public String GetMsg(String key)
    {
        String value = redisTemplate.opsForValue().get(key)==null ? "":redisTemplate.opsForValue().get(key).toString();
        System.out.println(key+":"+value);
        return value;
    }

    @Override
    public boolean GetState(String key)
    {
        String value = redisTemplate.opsForValue().get(key)==null ? "":redisTemplate.opsForValue().get(key).toString();
        System.out.println(key+":"+value);
        if(value=="1" || value.equals("1"))
        {
            //System.out.println("控制");
            return true;
        }
        else {
            //System.out.println("非控制");
            return false;
        }
    }

}
